package cn.minimelon.solon.service.cert.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.minimelon.solon.constants.YesEnum;
import cn.minimelon.solon.domain.cert.*;
import cn.minimelon.solon.mapper.cert.HosAttachMapper;
import cn.minimelon.solon.mapper.cert.HosRegDetailMapper;
import cn.minimelon.solon.mapper.cert.HosRegInfoMapper;
import cn.minimelon.solon.service.cert.QcHosRegInfoService;
import cn.minimelon.solon.utils.ShortIdGen;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.solon.annotation.Db;
import org.noear.solon.annotation.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class QcHosRegInfoServiceImpl implements QcHosRegInfoService {
    @Db("writer")
    private HosRegInfoMapper hosRegInfoMapper;

    @Db("writer")
    private HosRegDetailMapper hosRegDetailMapper;

    @Db("writer")
    private HosAttachMapper hosAttachMapper;

    @Override
    public void saveOne(HosRegInfoVO regInfo) {
        String id = ShortIdGen.getInstance().getId();
        // 保存附件
        if (CollUtil.isNotEmpty(regInfo.getAttachList())) {
            int num = 0;
            for (QcHosAttach attach : regInfo.getAttachList()) {
                attach.setId(IdUtil.fastUUID());
                attach.setCertId(id);
                attach.setHosId(regInfo.getHosId());
                attach.setCertType("3");
                attach.setCertKind(regInfo.getRegKind());
                attach.setActiveFlag(YesEnum.YES.code());
                attach.setDelFlag(YesEnum.NO.code());
                attach.setCreateTime(new Date());
                attach.setLastModified(new Date());
                attach.setSortNum(num++);
            }
            hosAttachMapper.insertBatch(regInfo.getAttachList());
        }
        regInfo.setId(id);
        if (StrUtil.isEmpty(regInfo.getMasterId())) {
            regInfo.setMasterId(id);
        }
        // 保存注册证
        regInfo.setActiveFlag(YesEnum.YES.code());
        regInfo.setCreateTime(new Date());
        regInfo.setLastModified(new Date());
        regInfo.setVersion(0);
        hosRegInfoMapper.insert(regInfo);
    }

    @Override
    public List<HosRegDetailVO> selectDetailList(RegDetailVO query) {
        return hosRegInfoMapper.selectDetailListByCond(query);
    }

    @Override
    public void saveDetail(HosRegInfoVO regInfo) {
        // 保存注册证关联产品
        if (CollUtil.isNotEmpty(regInfo.getDetailList())) {
            String pid = regInfo.getId();
            List<QcHosRegDetail> detailList = new ArrayList<>();
            for (HosRegDetailVO ui : regInfo.getDetailList()) {
                QcHosRegDetail detail = buildRegDetail(regInfo.getCreateUser(), ui, pid);
                detailList.add(detail);
            }
            hosRegDetailMapper.insertBatch(detailList);
        } else {
            throw new RuntimeException("未选择关联产品");
        }
    }

    @Override
    public void deleteHosRegDetailByPid(HosRegInfoVO curr) {

    }

    @Override
    public void update(HosRegInfoVO update) {

    }

    private QcHosRegDetail buildRegDetail(String userId, QcHosRegDetail ui, String pid) {
        QcHosRegDetail detail = new QcHosRegDetail();
        detail.setId(IdUtil.getSnowflakeNextIdStr());
        detail.setPid(pid);
        detail.setProvGoodsId(ui.getProvGoodsId());
        detail.setBasGoodsId(ui.getBasGoodsId());
        detail.setActiveFlag(YesEnum.YES.code());
        detail.setCreateTime(new Date());
        detail.setCreateUser(userId);
        detail.setLastModified(new Date());
        detail.setLastModifiedUser(userId);
        detail.setVersion(0);
        return detail;
    }
}
